﻿using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Zhaoxi.GrpcService
{
	//因为这边涉及到asp.ent core 里面的源代码
	public class UpdateService : IHostedService
	{
		Task updateTask = null;

		CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

		public Task StartAsync(CancellationToken cancellationToken)
		{
			updateTask = Task.Run(() =>
			{
				while (cancellationTokenSource.Token.IsCancellationRequested == false)
				{
					//心跳
					//Console.WriteLine(DateTime.Now + ": Executed");
					// 托管代码
					// 代码提供，加助教去要代码和视频
					// 组件
					RedisHelper.Heartbeat();
					Task.Delay(5000).Wait();
				}
			});

			return Task.CompletedTask;
		}

		public Task StopAsync(CancellationToken cancellationToken)
		{
			cancellationTokenSource.Cancel();

			return Task.CompletedTask;
		}
	}
}
